package com.ruoyi.develop.factory.factoryTestZn.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.develop.factory.factoryProductStore.domain.FactoryProductStore;
import com.ruoyi.develop.factory.factoryTestZnDetail.domain.FactoryTestZnDetail;
import com.ruoyi.develop.factory.factoryTestZnDetail.service.IFactoryTestZnDetailService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.factory.factoryTestZn.domain.FactoryTestZn;
import com.ruoyi.develop.factory.factoryTestZn.service.IFactoryTestZnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出厂试验-镀锌质量Controller
 * @author lmm
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/factoryTestZn/factoryTestZn")
public class FactoryTestZnController extends BaseController {
    @Autowired
    private IFactoryTestZnService factoryTestZnService;
    @Autowired
    private IFactoryTestZnDetailService factoryTestZnDetailService;



    /**
     * 查询出厂试验-镀锌质量列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZn:query')")
    @GetMapping("/list")
    public TableDataInfo list(FactoryTestZn entity) {
        startPage();
        List<FactoryTestZn> list = factoryTestZnService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 根据到货查 镀锌质量列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZn:query')")
    @GetMapping("/listByFahuoDetail")
    public TableDataInfo listByFahuoDetail(FactoryTestZn entity) {
        startPage();
        List<FactoryTestZn> list = factoryTestZnService.selectListByFahuoDetail(entity);
        return getDataTable(list);
    }



    /**
     * 导出出厂试验-镀锌质量列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZn:export')")
    @Log(title = "出厂试验-镀锌质量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FactoryTestZn entity) {
        List<FactoryTestZn> list = factoryTestZnService.selectList(entity);
        ExcelUtil<FactoryTestZn> util = new ExcelUtil<FactoryTestZn>(FactoryTestZn. class);
        util.exportExcel(response, list, "出厂试验-镀锌质量数据");
    }

    /**
     * 获取出厂试验-镀锌质量详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZn:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(factoryTestZnService.selectById(id));
    }

    /**
     * 新增出厂试验-镀锌质量
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZn:add')")
    @Log(title = "出厂试验-镀锌质量", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FactoryTestZn entity) {
        List<FactoryTestZnDetail> detailList = entity.getDetailList();
        BigDecimal total = BigDecimal.ZERO;
        for(FactoryTestZnDetail detail : detailList){
            // 求和
            total =  total.add(detail.getZnThick());
        }
        BigDecimal avg = total.divide(new BigDecimal(detailList.size()),2,BigDecimal.ROUND_HALF_UP);
        entity.setZnThick(avg);
        int i = factoryTestZnService.insert(entity);
        for (FactoryTestZnDetail detail : entity.getDetailList()) {
            detail.setTestId(entity.getId());
            factoryTestZnDetailService.insert(detail);
        }
        return toAjax(i);
    }

    /**
     * 修改出厂试验-镀锌质量
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZn:edit')")
    @Log(title = "出厂试验-镀锌质量", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody FactoryTestZn entity) {
        // 先删除detail
        factoryTestZnDetailService.deleteByTestId(entity.getId());
        List<FactoryTestZnDetail> detailList = entity.getDetailList();
        BigDecimal total = BigDecimal.ZERO;
        for(FactoryTestZnDetail detail : detailList){
            total =  total.add(detail.getZnThick());
            detail.setTestId(entity.getId());
            factoryTestZnDetailService.insert(detail);
        }
        BigDecimal avg = total.divide(new BigDecimal(detailList.size()),2,BigDecimal.ROUND_HALF_UP);
        entity.setZnThick(avg);
        int i = factoryTestZnService.update(entity);
        return toAjax(i);
    }

    /**
     * 删除出厂试验-镀锌质量
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZn:remove')")
    @Log(title = "出厂试验-镀锌质量", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(factoryTestZnService.deleteByIds(ids));
    }
}
